package com.zhu.pre.service.impl;


import com.sanploy.common.conf.PubliceVariable;
import com.sanploy.common.domain.Cart;
import com.sanploy.common.domain.User;
import com.sanploy.common.domain.vo.CartVo;
import com.sanploy.common.enums.Meta;
import com.sanploy.common.util.CommonUtil;
import com.sanploy.common.vo.BaseException;
import com.zhu.pre.mapper.CartMapper;
import com.zhu.pre.service.CartService;
import com.zhu.pre.service.GoodsAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * Created by zhu yingzhi on 2017/9/6.
 * @author yingzhi zhu
 * 购物车逻辑处理接口实现类
 */
@Service
public class CartServiceImpl implements CartService {


    @Autowired
    private CartMapper cartMapper;


    @Autowired
    private GoodsAttrService goodsAttrService;

    @Autowired
    private RedisTemplate redisTemplate;

    //TODO 添加尺寸价格
    @Override
    public int addCart(Cart cart,String token) throws BaseException {
        cart.setAdd_time(CommonUtil.getTimeNormal());
        cart.setUser_id(Optional.ofNullable((User)redisTemplate.opsForValue().get(token)).
                orElseThrow(()->new BaseException(Meta.AuthFail)).getId());
        if (cartMapper.insertCart(cart)< PubliceVariable.MinEffectRow){
            throw new BaseException(Meta.InsertFail);
        }
        return PubliceVariable.SuccessfulInt;
    }

    //TODO 购物车逻辑判断功能实现：，如商品没货了下架了，及变为不可购买，商品价格变动等。cart存在Redis中 返回库存 优化代码
    @Override
    public List<CartVo> findUserCart(String token) throws BaseException {
        Long userId = Optional.ofNullable((User)redisTemplate.opsForValue().get(token)).
                orElseThrow(()->new BaseException(Meta.AuthFail)).getId();
        List<CartVo> cartVos = cartMapper.selectCart(userId);
        for (CartVo cartVo:cartVos){
            //价格等于商品价格加上 商品的规格分类的价格
            goodsAttrService.findAttrArr(cartVo.getGoods_attrs_id().split(",")).forEach((item)->
                    cartVo.setGoods_price(cartVo.getGoods_price()+item.getIncrease_price()));
        }

        return cartVos;
    }

    @Override
    public int editCart(Long cart_id, String token, int number) throws BaseException {

        if (cartMapper.updateCartNumber(cart_id,number,Optional.ofNullable((User)redisTemplate.opsForValue().get(token)).
                orElseThrow(()->new BaseException(Meta.AuthFail)).getId())< PubliceVariable.MinEffectRow){
            throw new BaseException(Meta.UpdateFail);
        }

        return PubliceVariable.SuccessfulInt;
    }

    @Override
    public int countCart(String token) throws BaseException {
        return cartMapper.countCart(Optional.ofNullable((User)redisTemplate.opsForValue().get(token)).
                orElseThrow(()->new BaseException(Meta.AuthFail)).getId());
    }

    @Override
    public int deleteCart(String token, String[] cart_ids) throws BaseException {

        return cartMapper.deleteCart(Optional.ofNullable((User)redisTemplate.opsForValue().get(token)).
                orElseThrow(()->new BaseException(Meta.AuthFail)).getId(),cart_ids);
    }

}
